/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.Map;

import net.sourceforge.perforce.api.IError;
import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.internal.Policy;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * @version $Revision: 45 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  * 
  * TODO: Replace MultiStatus. Create MultiStatus by calling getCommand etc.
  */
public abstract class AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the status of the operation. */
	private MultiStatus status;
	/** Stores the progress monitor. */
	private IProgressMonitor monitor;
	/** flag indicating whether an operation is currently active or not. */
	private boolean inOperation;
	
	// ------------------------------------------------------------------------- Public Constructors

	/** Constructor for AbstractPerforceCommand. 
	  * 
	  * @param status the status object.
	  */
	AbstractPerforceCommand(MultiStatus status)
	{
		this(status, null);
	}
		
	/** Constructor for AbstractPerforceCommand. 
	  * 
	  * @param status the status object.
	  * @param monitor the progress monitor.
	  */
	AbstractPerforceCommand(MultiStatus status, IProgressMonitor monitor)
	{
		this.status = status;
		this.monitor = Policy.monitorFor(monitor);
	}
	
	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#handleError(Error) */
	public boolean handleError(IError error) 
	{
		outputError(error.format(IError.EF_PLAIN));
		return true;
	}

	/** @see IClientUserDelegate#inputData() */
	public String inputData(IError error) 
	{
		throw new UnsupportedOperationException();
	}

	/** @see IClientUserDelegate#message(Error) */
	public boolean message(IError error) 
	{
		if (error.isInfo())
		{
			String infoString = error.format(IError.EF_PLAIN);
			outputInfo((char) ('0' + error.getGeneric()), infoString);
		}
		else
		{
			handleError(error);
		}
		
		return true;
	}

	/** @see IClientUserDelegate#outputError(String) */
	public void outputError(String errorString) 
	{
		status.merge(PerforceStatus.decode(errorString, IStatus.ERROR));
	}

	/** @see IClientUserDelegate#outputInfo(char, String) */
	public void outputInfo(char level, String infoString) 
	{
		status.merge(PerforceStatus.decode(infoString, IStatus.ERROR));
	}

	/** @see IClientUserDelegate#outputStat(Map) */
	public void outputStat(Map strDict) 
	{
		throw new UnsupportedOperationException();		
	}

	/** @see IClientUserDelegate#outputBinary(char[]) */
	public void outputBinary(byte[] data) 
	{
		throw new UnsupportedOperationException();
	}

	/** @see IClientUserDelegate#outputText(String) */
	public void outputText(byte[] data) 
	{
		throw new UnsupportedOperationException();		
	}
	
	/** @see IClientUserDelegate#finished() */
	public void finished()
	{
	}		
	
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)
	
	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus() 
	{
		return status;
	}

	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole() 
	{
		return false;
	}
	
	// --------------------------------------------------------------------------- Protected Methods
	
	/** Call this method to retrieve the progress monitor. 
	  *
	  * @return the progress monitor. 
	  */
	protected IProgressMonitor getProgressMonitor()
	{
		return monitor;
	}
	
	
	/** Returns the associated MultiStatus object.
	  * 
	  * @return the status object.
	  */
	protected MultiStatus getMultiStatus()
	{
		return status;
	}
}
